﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RioStudio.Website.View.Admin.Service
{
    public partial class Detail : System.Web.UI.Page
    {
        #region "Property"

        private Guid ServiceID
        {
            get
            {
                if (Request.Params["key"] != null)
                {
                    return new Guid(Request.Params["key"]);
                }

                return Guid.Empty;
            }
        }

        private bool IsEdit
        {
            get
            {
                if (Request.Params["key"] != null)
                {
                    return true;
                }

                return false;
            }
        }

        #endregion

        private void GetData(Guid id)
        {
            var model = tData.Instance.ExecuteReader<RioStudio.Website.Model.Service>("Proc_GetServiceByID", id).FirstOrDefault();

            if (model != null)
            {
                txtServiceName.Text = model.ServiceName;
                txtSortOrder.Value = model.SortOrder;
                editorDetail.Content = model.Detail;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["key"] != null)
                {
                    GetData(this.ServiceID);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var model = new RioStudio.Website.Model.Service();

            model.ServiceName = txtServiceName.Text;
            model.SortOrder = (int)txtSortOrder.Value;
            model.Slug = model.ServiceName.RemoveSign4VietnameseString().Replace(" ", "-");
            model.Detail = editorDetail.Content;
            model.ServiceID = ServiceID;
            try
            {
                if (IsEdit)
                {
                    model.ModifiedBy = User.Identity.Name;
                    model.ModifiedDate = DateTime.Now;

                    tData.Instance.ExecuteNonQuery<RioStudio.Website.Model.Service>("Proc_UpdateService", model);
                }
                else
                {
                    model.CreatedBy = User.Identity.Name;
                    model.CreatedDate = DateTime.Now;

                    tData.Instance.ExecuteNonQuery<RioStudio.Website.Model.Service>("Proc_InsertService", model);
                }

                Response.Redirect("~/Admin/Service");
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("~/Admin/Service");
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}